<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">关键字：</label>
                                    <div class="layui-input-inline">
                                        <input id="menuEdtSearch" class="layui-input" type="text" placeholder="请输入关键字" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="menuBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>
            <!--数据表格-->
            <div class="table-bor">
                <table class="layui-table" id="menuTable" lay-filter="menuTable"></table>
            </div>
        </div>
    </div>
</div>
<style>
    .admin-set-menu .xm-body{
        height: 300px;
        overflow: hidden;
    }
    .admin-set-menu .xm-body .xm-body-tree{
        height: 100%;
        overflow: auto;
    }
</style>
<!-- 表格操作列 -->
<script type="text/html" id="menuTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="menuForm">
    <form lay-filter="menuForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">上级菜单</label>
            <div class="layui-input-block">
                <div id="parentId" class="xm-select-demo admin-set-menu"></div>
<!--                <select name="parentId" id="parentId" lay-search></select>-->
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入菜单名称" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单URL</label>
            <div class="layui-input-block">
                <div id="url" class="xm-select-demo"></div>
<!--                <input name="url" placeholder="请输入菜单URL" type="text" class="layui-input"-->
<!--                       maxlength="200"/>-->
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单权限</label>
            <div class="layui-input-block">
                <input name="authority" placeholder="请输入对应权限(多个用逗号分隔)" type="text" class="layui-input"
                       maxlength="100" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序号</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序号" type="number" class="layui-input"
                       max="100" lay-verify="required" required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单图标</label>
            <div class="layui-input-block">
                <input id="iconFonts" name="icon" lay-filter="iconFonts" placeholder="请输入菜单图标" type="text" class="layui-input"
                       maxlength="80"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="menuFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'config', 'IconFonts', 'tableTreeDj'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var config = layui.config;
        var IconFonts = layui.IconFonts;
        var tableTree = layui.tableTreeDj;

        var mMenus = [];  // 全部菜单
        var urlData = [];  // 全部页面URL

        //渲染表格
        var menuIns = tableTree.render({
            id: '#menuTable',
            elem: '#menuTable',
            url: config.base + 'user/admin/menu/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: false,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'name', align:'left', title: '名称'},
                {field: 'url', align:'center', title: 'URL', width: 300},
                {field: 'authority', align:'center', title: '对应权限', width: 300},
                {field: 'sortNumber', align:'center', title: '排序'},
                {field: 'createTime', align:'center', title: '创建时间'},
                {align: 'center', toolbar: '#menuTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {}
        }, {keyPid: "parentId", title: "name"});

        // 添加按钮点击事件
        $('#menuBtnAdd').click(function () {
            showEditModel();
        });

        // 搜索按钮点击事件
        $('#menuBtnSearch').click(function () {
            var keyword = $('#menuEdtSearch').val();
            menuIns.reload({where: {keyword: keyword, page: false}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            menuIns.reload({where: {keyword: '', page: false}}, 'data');
        });

        // 工具条点击事件
        table.on('tool(menuTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('user/admin/menu/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 显示编辑弹窗
        function showEditModel(menu) {
            // debugger;
           var openIndex= admin.open({
                type: 1,
                area: '600px',
                offset: ['130px','650px'],
                title: (menu ? '修改' : '添加') + '菜单',
                content: $('#menuForm').html(),
                scrollbar: false,
                success: function (layero, index) {

                    //图标选择器
                    IconFonts.render({
                        // 选择器，推荐使用input
                        elem: '#iconFonts', //选择器ID
                        // 数据类型：fontClass/layui_icon，
                        type: 'fontClass',
                        // 是否开启搜索：true/false
                        search: true,
                        // 是否开启分页
                        page: true,
                        // 每页显示数量，默认12
                        limit: 24,
                        // 点击回调
                        click: function (data) {
                            //console.log(data);
                        }
                    });

                    /**
                     * 默认选选中图标
                     */
                    IconFonts.checkIcon("iconFonts", menu && menu.icon ? menu.icon : "","fontClass");
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');  // 禁止出现滚动条

                    var parentIdSelect = xmSelect.render({
                        el: '#parentId',
                        name: 'parentId',
                        model: { label: { type: 'text' } },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            strict: false,
                            expandedKeys: [ -1 ],
                        },
                        filterable: true,
                        height: 'auto',
                        data(){
                            return [
                                {name: '顶层分类', value: -1, children: mMenus}
                            ]
                        }
                    });

                    var urlSelect = xmSelect.render({
                        el: '#url',
                        name: 'url',
                        radio: true,
                        clickClose: true,
                        filterable: true,
                        prop: {
                            name: 'pageUrl',
                            value: 'pageUrl',
                        },
                        model: {
                            label: {
                                type: 'text',
                                text: {
                                    //左边拼接的字符
                                    left: '',
                                    //右边拼接的字符
                                    right: '',
                                    //中间的分隔符
                                    separator: ', ',
                                },
                            }
                        },
                        data: urlData,
                        on: function(data){
                            var change = data.change;
                            $('input[name="authority"]').val(change[0].authority);
                            console.log(change);
                        },
                    });

                    if(menu){
                        parentIdSelect.setValue([ menu.parentId ]);
                        urlSelect.setValue([ menu.url ]);
                        tableTree.deleteIconAndIndent(menu);
                    }

                    form.render("select");
                    form.val('menuForm', menu);

                    // 表单提交事件
                    form.on('submit(menuFormSubmit)', function (data) {
                        layer.load(2);
                        admin.req(menu ? 'user/admin/menu/update' : 'user/admin/menu/add', data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                location.reload();
                                layer.msg(res.msg, {icon: 1});
                                layer.close(openIndex);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, menu ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 获取全部菜单
        admin.req('user/admin/menu/tree', {}, function (res) {
            layer.closeAll('loading');
            if (res.code == 200) {
                mMenus = res.data;
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'get');

        // 获取页面URL
        admin.req('user/admin/menu/pageUrlList', {}, function (res) {
            layer.closeAll('loading');
            if (res.code == 200) {
                urlData = res.data;
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'get');

    });
</script>